from flask import render_template, flash, redirect, request
from app import app
from .forms import LoginForm
import requests
from subprocess import call
from app.model.main import Modify_config, Modify_parameters, Modify_const, Modify_design

#@app.route('/')
#@app.route('/index')
#def index():
#    user = { 'nickname': 'Miguel' } # fake user
#    posts = [
#        {
#            'author' : { 'nickname' : 'John' },
#            'body' : 'Beautiful day in Portland!!'
#        },
#        {
#            'author' : { 'nickname' : 'Susan' },
#            'body' : 'The Avengers movie was so cool!!'
#        }
#    ]
#    return render_template("index.html",
#                           title = 'Home',
#                           user = user,
#                           posts = posts
#                          )
#
#@app.route('/login', methods = ['GET','POST'])
#def login():
#    form = LoginForm()
#    if form.validate_on_submit():
#        flash('Login requested for OpenID="' + form.openid.data + '",remember_me= ' + str(form.remember_me.data))
#        return redirect('/index')
#    return render_template('login.html',
#                          title = 'Sign In',
#                          form = form,
#                          providers = app.config[ 'OPENID_PROVIDERS' ]
#                          )

@app.route('/')
def config():
    return render_template("index.html"
                          )

@app.route('/www_config',methods = ['GET','POST'])
def www_config():
    if request.method == 'POST':
        text = request.form.to_dict()
        list = []
        if('check' in text.keys()):
            res = Modify_config()
            list.append(str(res))
            return render_template('modify_result.html',
                                  list = list
                                  )
        else:
            res = ['暂时不能进行推送操作!!!!']
            return render_template('modify_result.html',
                                   list = res
                                    )
        
    return render_template("www_config.html"
                          )

@app.route('/api_config',methods = ['GET','POST'])
def api_config():
    parameters = 'Modify_parameters'
    par_const = 'Modify_const'
    design_config = 'Modify_design'

    if request.method == 'POST':
        text = request.form.to_dict()
        list=[]
        for key in text:
            res = key.find('check')
            if res == 0:
                cmd = eval(text[key])
                cmd2 = eval(cmd)()
                list.append(cmd2)
                
            else:
                res = ['暂时不能进行推送操作!!!!']
                return render_template('modify_result.html',
                                      list = res
                                      )

        return render_template("modify_result.html",
                               list = list
                               )            
    return render_template("api_config.html"
                          )

@app.route('/cloudsearch',methods = ['GET','POST'])
def cloudsearch_config():
#    parameters = 'Modify_parameters'
#    par_const = 'Modify_const'
#    design_config = 'Modify_design'
#
    if request.method == 'POST':
#        text = request.form.to_dict()
#        list=[]
#        for key in text:
#            res = key.find('check')
#            if res == 0:
#                cmd = eval(text[key])
#                cmd2 = eval(cmd)()
#                list.append(cmd2)
#                
#            else:
        res = ['暂时不能进行操作!!!!']
        return render_template('modify_result.html',
                               list = res
                              )

    return render_template("cloudsearch_config.html")            

@app.route('/zi')
def zi():
    return render_template("back/zi.html")
